class GestureDrag 
{
    private target:MyImg;  //目标对象
    private curPointID:number = -1;      //当前触摸ID
	private lastX:number;
	private lastY:number;
    
	public setTarget(target:MyImg):void
	{
    	this.target = target;
	}
	
	public start():void
	{
    	this.curPointID = -1;
    	this.target.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
	}
	
	public stop():void
	{
		if(!this.target) return;
    	this.curPointID = -1;
        this.target.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
        StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.onTouchMove,this);
        StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.onTouchEnd,this);
		StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this);
	}

	private onTouchBegin(e:egret.TouchEvent):void
	{
		e.stopPropagation();
    	if(this.curPointID != -1) return;
    	this.curPointID = e.touchPointID;
    	this.lastX = e.stageX;
    	this.lastY = e.stageY;
        StageUtils.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
        StageUtils.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		StageUtils.stage.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this);
	}
	
	private onTouchMove(e:egret.TouchEvent):void
	{
    	if(e.touchPointID != this.curPointID) return;
		this.target.x += e.stageX - this.lastX;
        this.target.y += e.stageY - this.lastY;
        this.lastX = e.stageX;
        this.lastY = e.stageY;
		this.target.adjustMove();
	}
	
	private onTouchEnd(e:egret.TouchEvent):void
	{
    	if(e.touchPointID != this.curPointID) return;
        this.curPointID = -1;
        StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.onTouchMove,this);
        StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.onTouchEnd,this);
		StageUtils.stage.removeEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this);
	}
	
	public destroy():void
	{
    	this.stop();
    	this.target = null;
	}
}
